//
//  NewRecordingAudio.m
//  TutorialsModule
//
//  Created by Augusto Guido on 14/10/09.
//  Copyright 2009 Moove-IT. All rights reserved.
//

#import "NewRecordingAudio.h"


@implementation NewRecordingAudio
@synthesize soundFileURL;
@synthesize recording;
@synthesize playing;
@synthesize soundRecorder;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

- (void) viewDidLoad {
	
    [super viewDidLoad];
	
	UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:nil];
	self.navigationItem.rightBarButtonItem = save;
	
    NSString *tempDir = NSTemporaryDirectory ();
	
    NSString *soundFilePath =
	
	[tempDir stringByAppendingString: @"sound.caf"];
	
	
	
    NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
	
    self.soundFileURL = newURL;
	
    [newURL release];
	
	
	
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
	
    audioSession.delegate = self;
	
    [audioSession setActive: YES error: nil];
	
	
	
    recording = NO;
	
    playing = NO;
	
}

-(IBAction) recordOrStop {	
	
	if (recording) {		
		
		[soundRecorder stop];		
		recording = NO;		
		self.soundRecorder = nil;		
		
		[recordOrStopButton setTitle: @"Record" forState: UIControlStateNormal];		
		[recordOrStopButton setTitle: @"Record" forState: UIControlStateHighlighted];
		
		[[AVAudioSession sharedInstance] setActive: NO error: nil];		
		
	} else {
		
		[[AVAudioSession sharedInstance]		 
		 setCategory: AVAudioSessionCategoryRecord		 
		 error: nil];		
		
		NSDictionary *recordSettings =
		
		[[NSDictionary alloc] initWithObjectsAndKeys:
		 
		 [NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
		 
		 [NSNumber numberWithInt: 'alac'], AVFormatIDKey,
		 
		 [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
		 
		 [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
		 
         nil];
		
		AVAudioRecorder *newRecorder =		
		[[AVAudioRecorder alloc] initWithURL: soundFileURL		 
									settings: recordSettings		 
									   error: nil];
		
		[recordSettings release];		
		self.soundRecorder = newRecorder;		
		[newRecorder release];		
		
		soundRecorder.delegate = self;		
		[soundRecorder prepareToRecord];		
		[soundRecorder record];
		
		[recordOrStopButton setTitle: @"Stop" forState: UIControlStateNormal];		
		[recordOrStopButton setTitle: @"Stop" forState: UIControlStateHighlighted];
		
		recording = YES;
		
	}
	
}

-(IBAction) playRecorded{

	NSURL * urlEmpty =[[NSURL alloc] initWithString:@""];
	if (!recording && ![soundFileURL  isEqual:urlEmpty]) {
		AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];	
		player.play;
	}
	[urlEmpty release];
	
};
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
